//
//  FFRecvVC.m
//  LLWallet
//
//  Created by tt on 2020/12/16.
//

#import "FFRecvVC.h"

@interface FFRecvVC ()

@property(nonatomic,strong) NSTimer *timer;
@property(nonatomic,assign) NSInteger refreshInterval;

@end

@implementation FFRecvVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.viewModel = [FFRecvVM new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.style = WKNavigationBarStyleWhite;
    
    self.navigationBar.backgroundColor =  [UIColor colorWithRed:239.0f/255.0f green:182.0f/255.0f blue:0.0f/255.0f alpha:1.0f];
    self.view.backgroundColor =  [UIColor colorWithRed:239.0f/255.0f green:182.0f/255.0f blue:0.0f/255.0f alpha:1.0f];
    
    __weak typeof(self) weakSelf = self;
    [self.viewModel requestQRCode].then(^{
        weakSelf.refreshInterval = 1;
        [weakSelf.timer fire];
    });
}

- (NSString *)langTitle {
    return LLang(@"二维码收款");
}

- (void)viewDidDisappear:(BOOL)animated
{
    if (nil != _timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (NSTimer*)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(qrcodeRefreshCountDown)
                                                userInfo:nil
                                                 repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer
                                     forMode:NSRunLoopCommonModes];
    }
    return _timer;
}


-(void) qrcodeRefreshCountDown {
    NSInteger  expire = self.viewModel.expire;
    self.refreshInterval++;
    if(expire<=0) {
        expire = 60;
    }
    __weak typeof(self) weakSelf = self;
    if(self.refreshInterval%(expire-10) == 0) {
        WKLogDebug(@"刷新二维码");
        [weakSelf.viewModel requestQRCode];
    }
}

- (void)dealloc {
    [self.timer invalidate];
    self.timer = nil;
}

-(void) viewConfigChange:(WKViewConfigChangeType)type {
    
}

@end
